/*
 * LCD_7032i.h
 *
 * Created: 7/1/2012 10:06:56 AM
 *  Author: Steve
 */ 

#include "LCD_7032i.h"

/****************************************************************************
 *					Delay			 										*
 ****************************************************************************/
void delay(unsigned int n)				//Delay subroutine
{
	unsigned int i,j;
	for (i=0; i < n ;i++)
  		for (j=0;j<350;j++)
  			{;}
}
/****************************************************************************
 *					Deprecated but may work									*
 ****************************************************************************/
void Show(i2c_port_t *the_port, unsigned char *text)
{
	int n;
	START(the_port);
	i2cPutbyte(the_port, LCD_ADDRESS);
	i2cPutbyte(the_port, DATA);
	for(n=0;n<16;n++){
		i2cPutbyte(the_port, *text);
		++text;
		}
	STOP(the_port);
}
/****************************************************************************
 *			Shift Entire Display Left - Moves HOME Off Screen				*
 ****************************************************************************/
void LCD_Display_Shift_Left(i2c_port_t *the_port)
{
	START(the_port);
	i2cPutbyte(the_port, LCD_ADDRESS);
	i2cPutbyte(the_port, COMMAND);
	i2cPutbyte(the_port, Function_Set | Eight_Bit_Mode | Double_Height_Off | Number_Of_Lines_2 | Instruction_Table_0);
	i2cPutbyte(the_port, Shift_Display | Shift_Screen | Shift_Left);
	STOP(the_port);
}
/****************************************************************************
 *			Shift Entire Display Right - Moves HOME Off Screen				*
 ****************************************************************************/
void LCD_Display_Shift_Right(i2c_port_t *the_port)
{
	START(the_port);
	i2cPutbyte(the_port, LCD_ADDRESS);
	i2cPutbyte(the_port, COMMAND);
	i2cPutbyte(the_port, Function_Set | Eight_Bit_Mode | Double_Height_Off | Number_Of_Lines_2 | Instruction_Table_0);
	i2cPutbyte(the_port, Shift_Display | Shift_Screen | Shift_Right);
	STOP(the_port);
}
/****************************************************************************
 *			Clear Display and Return to HOME								*
 ****************************************************************************/
void LCD_Clear_Home(i2c_port_t *the_port)
{
	START(the_port);
	i2cPutbyte(the_port, LCD_ADDRESS);
	i2cPutbyte(the_port, COMMAND);
//	i2cPutbyte(the_port, Function_Set | Eight_Bit_Mode | Double_Height_Off | Number_Of_Lines_2 | Instruction_Table_1);
	i2cPutbyte(the_port, Clear_Home);	
	STOP(the_port);
}
/****************************************************************************
 *			Move Cursor to HOME												*
 ****************************************************************************/
void LCD_Home(i2c_port_t *the_port)
{
	START(the_port);
	i2cPutbyte(the_port, LCD_ADDRESS);
	i2cPutbyte(the_port, COMMAND);
//	i2cPutbyte(the_port, Function_Set | Eight_Bit_Mode | Double_Height_Off | Number_Of_Lines_2 | Instruction_Table_1);
	i2cPutbyte(the_port, Home);	
	STOP(the_port);
}
/****************************************************************************
 *			Set Cursor to any Position										*
 ****************************************************************************/
void LCD_Set_Position(i2c_port_t *the_port, uint8_t line_in, uint8_t position)
{
uint8_t line = 0;
	if (line_in == 1)	line = Line1;
	if (line_in == 2)	line = Line2;
	position--;
	START(the_port);
	i2cPutbyte(the_port, LCD_ADDRESS);
	i2cPutbyte(the_port, COMMAND);
//	i2cPutbyte(the_port, Function_Set | Eight_Bit_Mode | Double_Height_Off | Number_Of_Lines_2 | Instruction_Table_1);
	i2cPutbyte(the_port, Set_DDRAM_Address | line | position);
	STOP(the_port);
}
/****************************************************************************
 *			Output a Single Character										*
 ****************************************************************************/
void LCD_Display_Char(i2c_port_t *the_port, unsigned char the_char)
{
	START(the_port);
	i2cPutbyte(the_port, LCD_ADDRESS);
	i2cPutbyte(the_port, DATA);
	i2cPutbyte(the_port, the_char);
	STOP(the_port);
}
/****************************************************************************
 *			Set Cursor to First Position of Top Line						*
 ****************************************************************************/
void LCD_Line1(i2c_port_t *the_port)
{
	START(the_port);
	i2cPutbyte(the_port, LCD_ADDRESS);
	i2cPutbyte(the_port, COMMAND);
//	i2cPutbyte(the_port, Function_Set | Eight_Bit_Mode | Double_Height_Off | Number_Of_Lines_2 | Instruction_Table_1);
	i2cPutbyte(the_port, Set_DDRAM_Address | Line1 | Position1);
	STOP(the_port);
}
/****************************************************************************
 *			Set Cursor to First Position of Bottom Line						*
 ****************************************************************************/
void LCD_Line2(i2c_port_t *the_port)
{
	START(the_port);
	i2cPutbyte(the_port, LCD_ADDRESS);
	i2cPutbyte(the_port, COMMAND);
//	i2cPutbyte(the_port, Function_Set | Eight_Bit_Mode | Double_Height_Off | Number_Of_Lines_2 | Instruction_Table_1);
	i2cPutbyte(the_port, Set_DDRAM_Address | Line2 | Position1);
	STOP(the_port);
}
/****************************************************************************
 *			Set Up character Generator Ram (Not Used Yet)					*
 ****************************************************************************/
void CGRAM_Setup(uint8_t cgram_chars[8][8], i2c_port_t *the_port)
{
	uint8_t	character = 0;
	uint8_t	line_in_char = 0;
	
 	START(the_port);
	i2cPutbyte(the_port, LCD_ADDRESS);
	i2cPutbyte(the_port, COMMAND);
	i2cPutbyte(the_port, Function_Set | Eight_Bit_Mode | Double_Height_Off | Number_Of_Lines_2 | Instruction_Table_0);		//go to instruction table 0
	i2cPutbyte(the_port, Set_CGRAM_Address);																				//Set CGRAM LCD_ADDRESS to 0x00
	STOP(the_port);
	
	START(the_port);
	i2cPutbyte(the_port, LCD_ADDRESS);
	i2cPutbyte(the_port, DATA);
	
	for ( character = 0; character <= 7; character++)				//8 characters
		for ( line_in_char = 0; line_in_char <= 7; line_in_char++)	//8 lines per character
			i2cPutbyte(the_port, cgram_chars[character][line_in_char]);
	
	STOP(the_port);
}
/****************************************************************************
 *			Initialization For ST7032i										*
 ****************************************************************************/
void LCD_Init(i2c_port_t *the_port)
{
START(the_port);
i2cPutbyte(the_port, LCD_ADDRESS);
i2cPutbyte(the_port, COMMAND);
i2cPutbyte(the_port, Function_Set | Eight_Bit_Mode | Double_Height_Off | Number_Of_Lines_2 | Instruction_Table_1);
i2cPutbyte(the_port, Bias_And_OscFreq | BiasHi | OscFreq_183Hz);
i2cPutbyte(the_port, Icon_Boost_And_Contrast_HI | Icon_On | Boost_On | Contrast_Hi_Value_1);
i2cPutbyte(the_port, Contrast_Low_Value_7);
i2cPutbyte(the_port, Follower_Control | Follower_On | Follower_Ratio_5);
i2cPutbyte(the_port, Display_Mode | Display_Enable | Cursor_Off | Cursor_Position_Disable);
i2cPutbyte(the_port, Clear_Home);
i2cPutbyte(the_port, Entry_Mode | Cursor_Increment | Display_Shift_Enable);
STOP(the_port);
}
/****************************************************************************
 *			Energize LCD Backlight											*
 ****************************************************************************/
void LCD_Backlight(uint8_t backlight_setting)
{
if (backlight_setting == ON )
		LCD_LED_PORT &= ~LCD_LED;
 else	
		LCD_LED_PORT |=  LCD_LED;
}
